/*
 * Created by JFormDesigner on Fri Jul 10 21:01:38 CST 2020
 */
package entity.JPanel;

import utils.FindBook;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 * @author
 */
public class SearchBook extends JPanel {
    private BufferedImage bg;
    private int width;
    private int height;
    private JComboBox<String> comboBox1;
    private String select = "";
    // 默认显示的表格
    public DefaultTableModel model = new DefaultTableModel();
    private JTextField search_jTextField = new JTextField();
    private JButton search_button = new JButton("搜索");

    public SearchBook(int width, int height) {
        setLayout(null);
        this.width = width;
        this.height = height;
        setSize(width, height);
        try {
            bg = ImageIO.read(UserLoginJPanel.class.getClassLoader().getResourceAsStream("resources/images/booksearch.jpg"));
        } catch (Exception e) {
            System.out.println("NULL");
        }
        initComponents();
    }

    /**
     * 绘制背景图片
     *
     * @param g
     */
    public void paintComponent(Graphics g) {
        g.drawImage(bg, 0, 0, width, height, null);
    }

    /**
     * 设置组件
     */
    private void initComponents() {
        //=====comboBox1=======
        comboBox1 = new JComboBox<>();
        //下拉框参数
        comboBox1.setModel(new DefaultComboBoxModel<>(new String[]{
                "\u8ba1\u7b97\u673a\u7c7b", "\u6587\u5b66", "\u827a\u672f\u7c7b",
                "\u519b\u4e8b", "\u54f2\u5b66", "\u7ecf\u6d4e", "\u5386\u53f2",
                "\u7f51\u7edc\u5c0f\u8bf4", "\u5176\u4ed6"
        }));
        // 表格
        model.addColumn("书名", new Vector<Integer>());
        model.addColumn("作者", new Vector<Integer>());
        model.addColumn("类型", new Vector<Integer>());
        model.addColumn("库存", new Vector<Integer>());
        JTable jTable = new JTable(model);
        jTable.setRowHeight(50);
        jTable.getColumnModel().getColumn(0).setPreferredWidth(250);
        jTable.getColumnModel().getColumn(1).setPreferredWidth(150);
        //表格内容设置
        jTable.setFont(new Font("宋体", Font.BOLD, 18));
        jTable.setBackground(Color.pink);
        //表格内容居中显示
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
        tcr.setHorizontalAlignment(JLabel.CENTER);
        jTable.setDefaultRenderer(Object.class, tcr);
        //======表头属性设置====
        jTable.getTableHeader().setBackground(new Color(0, 191, 255));
        jTable.getTableHeader().setFont(new Font("楷体", Font.BOLD, 25));
        //======滚动面板=======
        JScrollPane pane = new JScrollPane(jTable);
        pane.setBounds((width / 2) - 400, 300, 800, 600);
        //=====下拉框样式设置==========
        add(comboBox1);
        comboBox1.setFont(new Font("宋体", Font.BOLD, 20));
        comboBox1.setBounds((width / 2) - 400, 200, 150, 40);
        comboBox1.addItemListener((e) -> {
            model.setRowCount(0);// 清除原有行
            if (e.getStateChange() == e.SELECTED) {
                // 选择的下拉框选项
                select = (String) e.getItem();
                System.out.println(select);
                FindBook.findbookbytype(model, select);
                System.out.println("model.getRowCount()+" + model.getRowCount());
            }
        });
        //=============输入框============
        search_jTextField.setBounds((width / 2) - 100, 200, 300, 40);
        search_jTextField.setFont(new Font("楷体", Font.BOLD, 18));
        add(search_jTextField);
        //=============按钮=============
        search_button.setBounds(1200, 200, 160, 40);
        search_button.setBackground(new Color(225, 255, 255));
        search_button.setBorderPainted(false);
        search_button.setFocusPainted(false);
        search_button.setIcon(new ImageIcon("C:\\Users\\429762125\\Desktop\\图书管理系统图片\\icon\\搜索.png"));
        add(search_button);
        FindBook.allbook(model);
        System.out.println("m" + model.getRowCount());
        search_button.addActionListener((e) -> {
            model.setRowCount(0);// 清除原有行
            if (search_jTextField.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "NULL", "警告", JOptionPane.WARNING_MESSAGE);
            } else {
                FindBook.searchbykey(model, search_jTextField.getText().trim());
            }
        });
        //添加滚动面板
        add(pane);
    }
}